package seygo;



import tools.HexStringUtils;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import static tools.ByteUtil.HexString2Bytes;


/**
 * Socket通信
 *
 */
public class App
{
    public static void main( String[] args )
    {
        Socket socket = null;
        String strReturn = null;
        try {
            System.out.println("connecting...");
            socket = new Socket("10.2.176.228", 3001);
            System.out.println("connection success");
            while (true){
//                String str = "FF 08 11 00 01 00 00 00 08 A5 73"; //发送的16进制字符串
                String str = "FF 08 11 00 01 00 00 00 16 AD F3"; //发送的16进制字符串(写入信息)
                byte[] bytes = hexStringToByteArray(str);
                System.out.println(bytes);
                OutputStream os = socket.getOutputStream();
                os.write(bytes);
                //输入流
                InputStream in=socket.getInputStream();
                //接收服务器的响应
                int line = 0;
                byte[] buf = new byte[1024];
                //接收收到的数据
                int len = in.read(buf);
                System.out.println(strReturn);
                strReturn= BinaryToHexString(buf);
                byte[] b = HexString2Bytes(strReturn.trim().replace(" ",""));
                String st = new String(b, StandardCharsets.US_ASCII);
//
//                String s = strReturn;

                String hex = HexStringUtils.toHexString(st.getBytes(StandardCharsets.ISO_8859_1));

                System.out.println("原字符串:" + hex);

                System.out.println("还原:" +strReturn );

//
//                String b2 = "0x323334357372722C";
                String var = strReturn.replace("FF 1C 11 80 01 00 00 ","");
                 var = var.replace(" ","");

                System.out.println(var);
                StringBuffer sbResult = new StringBuffer(var); //String转换为StringBuffer
                byte[] bytes2 = hexStringToByteArray("0x"+sbResult);
                String st2 = new String(bytes2,StandardCharsets.ISO_8859_1);
//                System.out.println(st);
//                FF 1C 11 80 01 00 00

            System.out.println(Thread.currentThread().getName() + "获取返回的数据:" + st2.substring(1,512));
                //os.close();
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {

                }
            }
        }
    }

    /**
     * 16进制表示的字符串转换为字节数组
     *
     * @param hexString 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    /**
     * 将字节数组转换成十六进制的字符串
     *
     * @return
     */
    public static String BinaryToHexString(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + " ";
        }
        return result;
    }

}
